<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
class NewsTagController extends AdminbaseController{
	function _initialize() {
		parent::_initialize();
	}
	
	function index(){
 		$userName = I('param.userName');
		$sex = I('param.sex');
		if($userName){
			$where = "&userName=$userName"; 
			
		}
		if($sex !== '全部'){
			$where .= "&sex=$sex";
				
		}
		$trtotal = $this->curl_http(C('news_url').'newsTag/getNewsTagList?token='.$_SESSION["TOKEN"].'&curPage=1&pageSize=1'.$where);
		$page = $this->page($trtotal['ReturnData']['page']['totalCount'],20);
		$result = $this->curl_http(C('news_url').'newsTag/getNewsTagList?token='.$_SESSION["TOKEN"]."&curPage=".$page->Current_page."&pageSize=".$page->Page_size.$where);
		$this->assign("data", $result['ReturnData']['data']);
		$this->assign("page", $page->show('Admin')); 
		$this->display();
	}
    function add(){
        $newsClass = $this->curl_http(C('news_url').'newsCategory/getNewsCategoryList?token='.$_SESSION["TOKEN"]);
        $this->assign("newsClass", $newsClass['ReturnData']['data']);
        $this->display();
    }
    function add_post(){
        $params = array(
            'categoryId' => I("param.categoryId", 0),
            'tagName' => I("param.tagName", 0),
            'sortIndex' => I("param.sortIndex", 0),
        );
        if (!$params['categoryId']) {
            $this->error("标题id不能为空！");
        }
        if (!$params['tagName']) {
            $this->error("标签名称不能为空！");
        }
        $fields = '&categoryId=' . $params['categoryId'] . '&tagName=' . $params['tagName'] . '&sortIndex=' . $params['sortIndex'];
        
        $result = $this->curl_http(C('news_url').'newsTag/addNewsTag?token='.$_SESSION["TOKEN"],$fields);
        if($result['Success']){
            $this->success("添加成功！", U("Admin/NewsTag/index"));
        }else{
            return $this->error($result['Message']);
        }
    
    }
    function edit(){
        $this->assign("tagId", I("param.tagId", 0));
        $this->assign("categoryId", I("param.categoryId", ""));
        $this->assign("tagName", I("param.tagName", ""));
        $this->assign("sortIndex", I("param.sortIndex", ""));
        $newsClass = $this->curl_http(C('news_url').'newsCategory/getNewsCategoryList?token='.$_SESSION["TOKEN"]);
        $this->assign("newsClass", $newsClass['ReturnData']['data']);
        $this->display();
    }
    function edit_post(){
        $params = array(
            'tagId' => I("param.tagId", 0),
            'categoryId' => I("param.categoryId", 0),
            'tagName' => I("param.tagName", 0),
            'sortIndex' => I("param.sortIndex", 0),
        );
        if (!$params['categoryId']) {
            $this->error("标题id不能为空！");
        }
        if (!$params['tagName']) {
            $this->error("标签名称不能为空！");
        }
        $fields = '&tagId=' . $params['tagId'] .'&categoryId=' . $params['categoryId'] . '&tagName=' . $params['tagName'] . '&sortIndex=' . $params['sortIndex'];
        $result = $this->curl_http(C('news_url').'newsTag/modifyNewsTag?token='.$_SESSION["TOKEN"],$fields);
        if($result['Success']){
            $this->success("修改成功！", U("Admin/NewsTag/index"));
        }else{
            return $this->error($result['Message']);
        }
    
    }
    function del(){
        $tagIds = I("param.tagId",0);
        $result = $this->curl_http(C('news_url').'newsTag/delNewsTag?token='.$_SESSION["TOKEN"].'&tagIds='.$tagIds);
        if($result['Success']){
            $this->success("删除成功！", U("Admin/NewsTag/index"));
        }else{
            return $this->error($result['Message']);
        }
    }
    public function listorders() {
        if(!empty($_POST['listorders'])){
            foreach($_POST['listorders'] as $key=>$val){
    
                $result = $this->curl_http(C('news_url').'newsTag/modifyNewsTag?token='.$_SESSION["TOKEN"].'&tagId='.$key.'&sortIndex='.$val);
                if($result['ResultCode'] != '0000'){
                    $this->error("排序更新失败！");
                }
            }
            $this->success("排序更新成功！");
        }
    }
}